<template>
  <section>
    <div class="img" v-if="pic" ref="pic">
      点击添加图片
      <input type="file" accept="image/*" @change="resizeImage($event)">
    </div>
    <div class="blank" v-else></div>
    <textarea cols="18" rows="2" placeholder="点击这里
定制你的问题" v-model="question.questionName"></textarea>
  <div v-for="(option, index) in question.optionVoList" :key="index"
  class="option">
    <svg class="icon" aria-hidden="true">
      <use xlink:href="#icon-gou"></use>
    </svg>
    <input type="text" :placeholder="'选项' + (index + 1)"
    v-model="question.optionVoList[index]">
    <svg class="icon delete" aria-hidden="true" @click="deleteOne(index)"
    v-if="question.optionVoList.length > 2">
      <use xlink:href="#icon-shanchuguanbicha2"></use>
    </svg>
  </div>
  <div class="option" @click="addNew">
    <svg class="icon" aria-hidden="true">
      <use xlink:href="#icon-gouhao"></use>
    </svg>
    <span>添加新选项</span>
  </div>
  </section>
</template>

<script>
import { checkQuestion } from '@/api/common'
import {uploadImage} from '@/api/image'

export default {
  name: 'survey2',
  data () {
    return {
      question: {
        questionName: '',
        sort: 0,
        type: 2,
        imgUrl: '',
        isSkip: 0,
        optionVoList: [
          '',
          ''
        ]
      }
    }
  },
  props: [
    'pic'
  ],
  methods: {
    addNew () {
      this.question.optionVoList.push('')
    },
    deleteOne (index) {
      this.question.optionVoList.splice(index, 1)
    },
    resizeImage (e) {
      let file = e.target.files[0]
      window.lrz(file)
      .then(rst => {
        this.$refs.pic.style.backgroundImage = `url(${rst.base64})`
        return rst
      })
      .then(rst => {
        uploadImage(rst.formData).then(rst => {
          let imageUrl = rst.data.data
          this.question.imgUrl = imageUrl
        })
      })
    },
    trans () {
      if (checkQuestion(this.question, 2)) {
        this.$emit('addSurvey', this.question)
        return true
      } else {
        return false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
section {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.img {
  position: relative;
  width: 750px;
  height: 750px;
  background-color: #4b539e;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 40px;
  color: #999;
  background-size: cover;
  input {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
  }
}
.blank {
  width: 100%;
  height: 100px;
}
textarea {
  text-align: center;
  margin: 30px auto;
  font-size: 48px;
  border: none;
  resize: none;
  &::-webkit-input-placeholder {
    text-align: center;
    color: #999;
  }
}
.option {
  width: 600px;
  display: flex;
  align-items: center;
  margin: 15px 0;
  position: relative;
}
.icon {
  width: 70px;
  height: 70px;
  color: #999;
}
.delete {
  position: absolute;
  right: 0;
  top: 0;
  width: 60px;
  height: 60px;
  color: #ff4f00;
}
input {
  width: 450px;
  font-size: 40px;
  border: none;
  padding-left: 20px;
  color: #999;
  &::-webkit-input-placeholder {
    color: #999;
  }
}
span {
  font-size: 40px;
  color: #666;
  padding-left: 20px;
}
</style>
